﻿using System;
using AppServer.Core;

namespace AppServer.Implement.SerialPort
{
    public class SerialPortSession:AbstractAppSession
    {
        System.IO.Ports.SerialPort m_Serail = null;

        public SerialPortSession(System.IO.Ports.SerialPort serial)
            : base(serial.PortName)
        {
            m_Serail = serial;
        }

        /// <summary>
        /// 发送命令
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>发送成功或失败</returns>
        public override bool Send(ICommand command)
        {
            try
            {
                byte[] buff=command.GetBytes();
                if (m_Serail.IsOpen)
                {
                    m_Serail.Write(buff, 0, buff.Length);
                }
                else
                {
                    return false;  //Error
                }
            }
            catch (Exception ex)
            {
                logger.Debug(ex);
                return false;
            }
            return true;
        }
    }
}
